#include <stdio.h>
#include "link_puc.h"

void add_data_ele(){
    printf("%c%s学生选课%s\n", eg, ty, ty);
    int stu_id, sub_id;
    printf("%c输入需要选课的学生学号：", eg);
    scanf("%d", &stu_id);
    BackCall call = get_stu_id(stu_id);
    if(call.code != 1){
        printf("%c没有该学生信息\n", eg);
        return;
    }
    printf("%c学号\t姓名\t已选课程 已获取学分\n", eg);
    Student stu = call.stus->stu;
    printf("%c%3d\t%-3s\t   %2d\t   %2.1f\t\n", \
           eg, stu.id, stu.name, stu.num_sub, stu.sum_credit);
    call = get_sub_by_not_stu(stu_id);
    if(call.code != 1){
        printf("%c暂无%s同学可选的课程\n", eg, stu.name);
        return;
    }
    printf("%c%s可选的课程如下：\n", eg, stu.name);
    printf("%c%s\t%-15s\t%s\t%s\t%s\t%s\n", \
                   eg, "编号", "课程名", "性质", \
                   "课时", "学分", "学生人数");
    Subjects *subs = call.subs;
    while(subs != NULL){
        Subject sub = subs->sub;
        printf("%c%3d\t%-15s\t%-3s\t%3d\t%.1f\t%2d/%-2d\t\n", \
                 eg, sub.id, sub.name, sub.nature, \
                 sub.period, sub.credit, sub.num, sub.max_num);
        subs = subs->next;
    }
    printf("%c请选择课程编号：", eg);
    scanf("%d", &sub_id);
    subs = call.subs;
    while(subs != NULL){
        if(subs->sub.id == sub_id){
            if(subs->sub.num >= subs->sub.max_num){
                printf("%c该课程学生数量已满，选课失败\n", eg);
                return;
            }else{
                break;
            }
        }
        subs = subs->next;
    }
    if(subs == NULL){
        printf("%c该 %d 课程编号不存在，选课失败\n", eg, sub_id);
        return;
    }
    if(put_sub_by_stu(stu, subs->sub).code != 1){
        printf("%c选课失败\n", eg);
        return;
    }
    printf("%c选课成功\n", eg);
    free_stu(stu);
    free_subs(subs);
}

void delete_data_ele(){
    printf("%c%s学生退课%s\n", eg, ty, ty);
    int stu_id, sub_id;
    printf("%c输入需要退课的学生学号：", eg);
    scanf("%d", &stu_id);
    BackCall call = get_stu_id(stu_id);
    if(call.code != 1){
        printf("%c没有该学生信息\n", eg);
        return;
    }
    printf("%c学号\t姓名\t已选课程 已获取学分\n", eg);
    Student stu = call.stus->stu;
    printf("%c%3d\t%-3s\t   %2d\t   %2.1f\t\n", \
           eg, stu.id, stu.name, stu.num_sub, stu.sum_credit);
    call = get_sub_by_stu(stu);
    if(call.code != 1){
        printf("%c暂无%s同学可退的课程\n", eg, stu.name);
        return;
    }
    printf("%c%s同学可退的课程如下：\n", eg, stu.name);
    printf("%c%s\t%-15s\t%s\t%s\t%s\t%s\n", \
                   eg, "编号", "课程名", "性质", \
                   "课时", "学分", "学生人数");
    Subjects *subs = call.subs;
    while(subs != NULL){
        Subject sub = subs->sub;
        printf("%c%3d\t%-15s\t%-3s\t%3d\t%.1f\t%2d/%-2d\t\n", \
                 eg, sub.id, sub.name, sub.nature, \
                 sub.period, sub.credit, sub.num, sub.max_num);
        subs = subs->next;
    }
    printf("%c请选择课程编号：", eg);
    scanf("%d", &sub_id);
    subs = call.subs;
    while(subs != NULL){
        if(subs->sub.id == sub_id){
            break;
        }
        subs = subs->next;
    }
    if(subs == NULL){
        printf("%c该 %d 课程编号不存在，退课失败\n", eg, sub_id);
        return;
    }
    if(remove_sub_by_stu(stu, subs->sub).code != 1){
        printf("%c退课失败，未知错误\n", eg);
        return;
    }
    printf("%c退课成功\n", eg);
    free_stu(stu);
    free_subs(subs);
}


void show_init_ele(){
    printf("%c学生选课(1)\n", eg);
    printf("%c学生退课(2)\n", eg);
    printf("%c退出选课(0)\n", eg);
    printf("%c请输入(0-2)：", eg);
}

int show_ele(){
    printf("%c%s选课系统%s\n", eg, ty, ty);
    int type;
    do{
        show_init_ele();
        scanf("%d", &type);
        getchar();
        if(type == 0){
            break;
        }
        switch(type){
        case 1: add_data_ele();
            break;
        case 2: delete_data_ele();
            break;
        default:
            break;
        }
    }while(1);
    return 1;
}
